package com.springbootdemo.demo.mail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;

/**
 * 发送HTML邮件
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class HtmlEmailTest {

    @Autowired
    private Environment environment;
    @Autowired
    private JavaMailSender mailSender;



    @Test
    public void sendHtmlMail() {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(environment.getProperty("spring.mail.username"));
            helper.setTo("15712058302@163.com");
            //helper.setTo("wyzshiwu@163.com");
            helper.setSubject("Html邮件");

            StringBuffer sb = new StringBuffer();
            sb.append("<h1>邮件标题</h1>")
                    .append("<p style='color:#F00'>红色</p>")
                    .append("<p style='text-align:right'>吴一折</p>");
            helper.setText(sb.toString(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailSender.send(message);
    }

}
